import 'package:flutter/material.dart';
import 'package:flutter_elf/_base/base_widget.dart';
import 'package:http/http.dart' as Http;
import 'package:flutter_elf/widgets/appbar_creator.dart';
import 'package:flutter_elf/utils/print_utils.dart';
import 'dart:convert' show json, utf8,base64;
void main(){
  runApp(MaterialApp(
    home: ExitPage(),
  ));
}

class ExitPage extends BaseWidget{
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return ExitPageState();
  }
}
class ExitPageState extends BaseWidgetState<ExitPage>{
  @override
  Widget buildView(BuildContext context) {
    // TODO: implement buildView
    return Column(
      children: <Widget>[
        RaisedButton(
          onPressed: ()async{
            String base = 'aHR0cHMlM0EvL2RlbmdqaWJhby5vc3MtY24tc2hhbmdoYWkuYWxpeXVuY3MuY29tL21kU2xyeS5qc29u';
            String url = utf8.decode(base64.decode(base)).replaceAll('%3A', ':');
            Http.Response res = await Http.get(url);
            Data data = Data.fromJson(json.decode(res.body));
            globalPrint(data);
          },
          child: Text('get'),
        )
      ],
    );
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return AppBarCreator.create(context, '测试http');
  }
}


class Data {

  List<Id> data;

  Data.fromParams({this.data});

  factory Data(jsonStr) => jsonStr == null ? null : jsonStr is String ? new Data.fromJson(json.decode(jsonStr)) : new Data.fromJson(jsonStr);

  Data.fromJson(jsonRes) {
    data = jsonRes['data'] == null ? null : [];

    for (var dataItem in data == null ? [] : jsonRes['data']){
      data.add(dataItem == null ? null : new Id.fromJson(dataItem));
    }
  }

  @override
  String toString() {
    return '{"data": $data}';
  }
}

class Id {

  String userId;

  Id.fromParams({this.userId});

  Id.fromJson(jsonRes) {
    userId = jsonRes['userId'];
  }

  @override
  String toString() {
    return '{"userId": ${userId != null?'${json.encode(userId)}':'null'}}';
  }
}

